package com.self.smallma.myblogweb.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.self.smallma.myblogweb.common.utils.PageUtils;
import com.self.smallma.myblogweb.entity.BlogEntity;
import com.self.smallma.myblogweb.entity.TypeEntity;
import com.self.smallma.myblogweb.service.BlogService;
import com.self.smallma.myblogweb.service.TypeService;
import com.self.smallma.myblogweb.vo.FirstPageBlogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TypeShowController {
    @Autowired
    TypeService typeService;

    @Autowired
    BlogService blogService;
    @GetMapping("/types/{id}")
    public String types(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,
                        @PathVariable("id") Long id, Model model){
        //查找所有的博客和分类
      List<TypeEntity> types =  typeService.getAllTypeAndBlog();

        //-1表示从首页导航点进来的
        if (id == -1){
            //直接显示第一个
            id = types.get(0).getTypeId();
        }
        model.addAttribute("types",types);
        System.out.println(types);
        //若id不等于-1表示从分类页点的，带上了分类的id
         //根据分类的id，查出对应的博客----是一个列表

        Map<String, Object> map = new HashMap<>();
        map.put("page",pageNum.toString());
        map.put("limit","5");

        PageUtils pageUtils = blogService.listByShow(map,id);
//        pageUtils.getList().forEach(System.out::println);
//        PageHelper.startPage(pageNum, 10000);
//        searchBlog<FirstPageBlogVo> searchBlog = new searchBlog<>(blogEntities);
//        model.addAttribute("searchBlog", searchBlog);
        model.addAttribute("searchBlog", pageUtils);
        System.out.println(pageUtils);
        model.addAttribute("activeTypeId", id);

        return "types";
    }
}
